package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.CartGoodsAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.EmptyRecyclerView;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsAllPriceBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCartAllGoodPriceModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCartGoodListModel;

public class GetCartGoodsActivity extends BaseActivity {

    EmptyRecyclerView goodcart_rv;
    private String shopid,userid;
    private List<CartGoodsBean> items;
    TextView cartgoodallprice,cartgoodall_count;
    private Dialog mdialog;
    private boolean up = false;

    private View mEmptyView;
    TextView cartgood_jiesuan;
    private String shopname;

    private String isbuy_flag;

    public android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
//                    CartPriceModel cartPriceModel=new CartPriceModel();
//                    cartPriceModel.showCartPrice(getid(),mrid,tListener);
                    //showToast("handler");
                    mdialog = WeiboDialogUtils.createLoadingDialog(GetCartGoodsActivity.this, "加载中...");
                    GetCartAllGoodPriceModel getCartAllGoodPriceModel = new GetCartAllGoodPriceModel();
                    getCartAllGoodPriceModel.getCartAllGoodPrice(userid,shopid,cartGoodsAllPriceBeanTListener);
                    break;
            }
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_get_cart_goods);
        mEmptyView = (View) findViewById(R.id.id_empty_view);
        goodcart_rv = (EmptyRecyclerView)findViewById(R.id.goodcart_rv);
        cartgoodallprice = (TextView)findViewById(R.id.cartgoodallprice);
        shopid=getIntent().getStringExtra("shopid");
        shopname=getIntent().getStringExtra("shopname");
        cartgood_jiesuan = (TextView)findViewById(R.id.cartgood_jiesuan);
        cartgoodall_count = (TextView)findViewById(R.id.cartgoodall_count);
        userid=getid();

        GetCartGoodListModel getCartGoodListModel = new GetCartGoodListModel();
        getCartGoodListModel.getCartGoodList(userid,shopid,goodsBeanListListener);
       // Toast.makeText(GetCartGoodsActivity.this,userid+"-----"+shopid,Toast.LENGTH_SHORT).show();
        GetCartAllGoodPriceModel getCartAllGoodPriceModel = new GetCartAllGoodPriceModel();
        getCartAllGoodPriceModel.getCartAllGoodPrice(userid,shopid,cartGoodsAllPriceBeanTListener);

    }

    @Override
    void initEvent() {
        cartgood_jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isbuy_flag.equals("0")){
                    Toast.makeText(GetCartGoodsActivity.this,"必须选中一件商品才可下单",Toast.LENGTH_SHORT).show();
                }
                else {
                    Intent intent = new Intent(GetCartGoodsActivity.this,BuyCartGoodsActivity.class);
                    intent.putExtra("shopid",shopid);
                    intent.putExtra("shopname",shopname);
                    startActivity(intent);
                }

            }
        });

    }

    @Override
    void initDate() {

    }

    TListener<CartGoodsAllPriceBean> cartGoodsAllPriceBeanTListener = new TListener<CartGoodsAllPriceBean>() {
        @Override
        public void onResponse(CartGoodsAllPriceBean cartGoodsAllPriceBean) {
            //Toast.makeText(GetCartGoodsActivity.this,cartGoodsAllPriceBean.getAllprice(),Toast.LENGTH_SHORT).show();
            cartgoodallprice.setText("￥"+String.valueOf(cartGoodsAllPriceBean.getAllprice()));
            cartgoodall_count.setText("选中"+cartGoodsAllPriceBean.getBuyflag_count()+"件");
            isbuy_flag = cartGoodsAllPriceBean.getBuyflag_count();
            DialogThridUtils.closeDialog(mdialog);
        }

        @Override
        public void onFail(String msg) {
            DialogThridUtils.closeDialog(mdialog);
        }
    };
    ListListener<CartGoodsBean> goodsBeanListListener = new ListListener<CartGoodsBean>() {
        @Override
        public void onResponse(List<CartGoodsBean> list) {
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(GetCartGoodsActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            goodcart_rv.setLayoutManager(linearLayoutManager);
            items=list;
            CartGoodsAdapter cartGoodsAdapter=new CartGoodsAdapter(GetCartGoodsActivity.this,items,R.layout.cart_goods_item,handler);
            goodcart_rv.setAdapter(cartGoodsAdapter);
            goodcart_rv.setEmptyView(mEmptyView);
            goodcart_rv.setItemAnimator(new DefaultItemAnimator());
        }
        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        up = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up) {
            initView();
            initEvent();
            initDate();
            up = false;
        }
    }
}
